package cn.zyq.ykb.controller.mgt.user;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.task.bo.UserUpgradeBo;
import cn.zyq.ykb.beans.task.bo.UserUptTeamBo;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.system.exception.BusinessException;
import cn.zyq.ykb.system.utils.MD5Util;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.zyq.ykb.system.ry.util.poi.ExcelUtil;
import cn.zyq.ykb.system.common.BaseController;
import cn.zyq.ykb.system.annotation.Log;
import cn.zyq.ykb.system.ry.enums.BusinessType;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.zyq.ykb.system.common.PageParam;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author SystemAuth
 * @date 2023-05-23
 */

@RestController(value = "MgtUserController")
@RequestMapping("/mgt/user")
@Api(tags = "用户接口")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    /**
     * 查询用户列表
     */
    @ApiOperation(value = "列表", response = User.class)
    @PostMapping("/list")
    public Object list(@RequestBody PageParam pageParam) {
        return this.userService.selectUserList(pageParam,this.getLoginSysAdmin());
    }

    /**
     * 导出用户列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "用户" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User. class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @ApiOperation(value = "详情", response = User.class)
    @GetMapping(value = "/{userID}")
    public Object getInfo(@PathVariable("userID") Integer userID) {
        return this.userService.selectUserByUserID(userID);
    }
    /**
     * 获取用户详细信息
     */
    @ApiOperation(value = "详情", response = User.class)
    @GetMapping(value = "/detail/{serviceID}")
    public Object getInfo(@PathVariable(value = "serviceID") String serviceID) {
        return this.userService.selectUserByServiceID(serviceID);
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public Object add(@RequestBody User user) {
        return this.userService.insertUser(user);
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "编辑")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public Object edit(@RequestBody User user) {
        return this.userService.updateUser(user);
    }

    /**
     * 删除用户
     */
    @ApiOperation(value = "删除")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIDs}")
    public Object remove(@PathVariable Integer[] userIDs) {
        return this.userService.deleteUserByUserIDs(userIDs);
    }

    /**
     * 查询所有服务商
     */
    @ApiOperation(value = "查询所有服务商")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @GetMapping("/getService")
    public Object getService() {
        return this.userService.getService();
    }

    /**
     * 升级
     */
    @ApiOperation(value = "升级")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/upgrade")
    public Object upgrade(@RequestBody UserUpgradeBo bo) {
        return this.userService.upgrade(bo,this.getLoginSysAdmin());
    }

    /**
     * 更换团队
     */
    @ApiOperation(value = "更换团队")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/uptTeam")
    public Object uptTeam(@RequestBody UserUptTeamBo bo) {
        return this.userService.uptTeam(bo,this.getLoginSysAdmin());
    }

    /**
     * 余额记录
     */
    @ApiOperation(value = "余额记录")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @GetMapping("/balanceRecord/{userID}")
    public Object balanceRecord(@PathVariable("userID") Integer userID) {
        return this.userService.balanceRecord(userID);
    }

    /**
     * 平台打款
     */
    @ApiOperation(value = "平台打款")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @GetMapping("/payment/{userID}/{amount}")
    public Object payment(@PathVariable("userID") Integer userID, @PathVariable("amount")BigDecimal amount) {
        return this.userService.payment(userID,amount,this.getLoginSysAdmin());
    }

    /**
     * 调整服务商下属团队长数量
     */
    @ApiOperation(value = "调整服务商下属团队长数量")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/uptTzdNumber")
    public Object uptTzdNumber(@RequestBody User user) {
        return this.userService.uptTzdNumber(user,this.getLoginSysAdmin());
    }

    /**
     * 注册用户数量
     *
     */
    @ApiOperation(value = "注册用户数量")
    @GetMapping("/getUserNumber")
    public Object getUserNumber() {
        return this.userService.getUserNumber(this.getLoginSysAdmin());
    }

    /**
     * 一周注册用户数量
     */
    @ApiOperation(value = "一周注册用户数量")
    @GetMapping("/getRegisterNumber/{time}")
    public Object getRegisterNumber(@PathVariable("time") String time) {
        return this.userService.getRegisterNumber(time,this.getLoginSysAdmin());
    }

    /**
     * 一周注册用户数量
     */
    @ApiOperation(value = "修改体验版账号")
    @GetMapping("/uptPassword/{password}")
    public Object uptPassword(@PathVariable("password") String password) {
        User user = new User();
        user.setPhone("17381570019");
        user.setDeleted(0);
        User user1 = this.userMapper.selectOne(user);
        if (BeanUtil.isEmpty(user1)){
            throw new BusinessException("体验账号不存在");
        }
        user1.setPassword(MD5Util.encode(password));
        return this.userMapper.updateByPrimaryKeySelective(user1);
    }


}
